package com.mbyte.easy.taskstudent.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mbyte.easy.qcbyclass.entity.QcbyClass;
import com.mbyte.easy.status.entity.Status;
import com.mbyte.easy.status.service.IStatusService;
import com.mbyte.easy.task.entity.Task;
import com.mbyte.easy.task.service.ITaskService;
import com.mbyte.easy.taskstudent.entity.TaskStudent;
import com.mbyte.easy.taskstudent.service.TaskIStudentService;
import com.mbyte.easy.common.controller.BaseController;
import com.mbyte.easy.common.web.AjaxResult;
import com.mbyte.easy.util.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.util.ObjectUtils;
import java.util.List;

/**
* <p>
* 前端控制器
* </p>
* @author lp
* @since 2019-03-11
*/
@Controller
@RequestMapping("/taskstudent/student")
public class TaskStudentController extends BaseController  {

    private String prefix = "taskstudent/student/";

    private String prefix1 = "task/task/";

    @Autowired
    private TaskIStudentService studentService;
    @Autowired
    private IStatusService statusService;
    @Autowired
    private ITaskService taskService;

    /**
    * 查询列表
    *
    * @param model
    * @param pageNo
    * @param pageSize
    * @param taskstudent
    * @return
    */
    @RequestMapping
    public String index(Model model,@RequestParam(value = "pageNo", required = false, defaultValue = "1") Integer pageNo,@RequestParam(value = "pageSize", required = false, defaultValue = "20") Integer pageSize, TaskStudent taskstudent) {
        Page<TaskStudent> page = new Page<TaskStudent>(pageNo, pageSize);
        QueryWrapper<TaskStudent> queryWrapper = new QueryWrapper<TaskStudent>();
        if(!ObjectUtils.isEmpty(taskstudent.getUserId())) {
            queryWrapper = queryWrapper.like("user_id",taskstudent.getUserId());
         }
        if(!ObjectUtils.isEmpty(taskstudent.getClassId())) {
            queryWrapper = queryWrapper.like("class_id",taskstudent.getClassId());
         }
        if(!ObjectUtils.isEmpty(taskstudent.getName())) {
            queryWrapper = queryWrapper.like("name",taskstudent.getName());
         }
        if(!ObjectUtils.isEmpty(taskstudent.getSex())) {
            queryWrapper = queryWrapper.like("sex",taskstudent.getSex());
         }
        if(!ObjectUtils.isEmpty(taskstudent.getAge())) {
            queryWrapper = queryWrapper.like("age",taskstudent.getAge());
         }
        if(!ObjectUtils.isEmpty(taskstudent.getPhone())) {
            queryWrapper = queryWrapper.like("phone",taskstudent.getPhone());
         }
        if(!ObjectUtils.isEmpty(taskstudent.getEmail())) {
            queryWrapper = queryWrapper.like("email",taskstudent.getEmail());
         }
        IPage<TaskStudent> pageInfo = studentService.pageInfo(page, queryWrapper,taskstudent);
        List<Status> statusList = statusService.list();
        model.addAttribute("searchInfo", taskstudent);
        model.addAttribute("statusList", statusList);
        model.addAttribute("pageInfo", new PageInfo(pageInfo));
        return prefix+"list";
    }

    /**
    * 添加跳转页面
    * @return
    */
    @GetMapping("addBefore")
    public String addBefore(){
        return prefix+"add";
    }
    /**
    * 添加
    * @param student
    * @return
    */
    @PostMapping("add")
    @ResponseBody
    public AjaxResult add(TaskStudent student){
        return toAjax(studentService.save(student));
    }
    /**
    * 添加跳转页面
    * @return
    */
    @GetMapping("editBefore/{id}")
    public String editBefore(Model model,@PathVariable("id")Long id,
                             @RequestParam(value = "pageNo", required = false, defaultValue = "1") Integer pageNo,
                             @RequestParam(value = "pageSize", required = false, defaultValue = "20") Integer pageSize,
                             String creatTimeSpace, String deleteTimeSpace, Task task){
        Page<Task> page = new Page<Task>(pageNo, pageSize);
        QueryWrapper<Task> queryWrapper = new QueryWrapper<Task>();
        if(!ObjectUtils.isEmpty(task.getTaskType())) {
            queryWrapper = queryWrapper.like("task_type",task.getTaskType());
        }
        if(!ObjectUtils.isEmpty(task.getTaskTitle())) {
            queryWrapper = queryWrapper.like("task_title",task.getTaskTitle());
        }
        if(!ObjectUtils.isEmpty(task.getTaskContent())) {
            queryWrapper = queryWrapper.like("task_content",task.getTaskContent());
        }
        if(!ObjectUtils.isEmpty(task.getTaskStatus())) {
            queryWrapper = queryWrapper.like("task_status",task.getTaskStatus());
        }
        if(!ObjectUtils.isEmpty(task.getUserId())) {
            queryWrapper = queryWrapper.like("user_id",task.getUserId());
        }
        if(!ObjectUtils.isEmpty(task.getCreatTime())) {
            queryWrapper = queryWrapper.like("creat_time",task.getCreatTime());
        }
        if(!ObjectUtils.isEmpty(task.getDeleteTime())) {
            queryWrapper = queryWrapper.like("delete_time",task.getDeleteTime());
        }
        if(!ObjectUtils.isEmpty(task.getDeleteStatus())) {
            queryWrapper = queryWrapper.like("delete_status",task.getDeleteStatus());
        }
        IPage<Task> pageInfo = taskService.selectAllTask(page, queryWrapper,id);
        model.addAttribute("creatTimeSpace", creatTimeSpace);
        model.addAttribute("deleteTimeSpace", deleteTimeSpace);
        model.addAttribute("searchInfo", task);
        model.addAttribute("pageInfo", new PageInfo(pageInfo));

        return prefix1+"list";
    }
    /**
    * 添加
    * @param student
    * @return
    */
    /**
     * 修改跳转页面
     * @return
     */
    @GetMapping("statusBefore/{id}")
    public String statusBefore(Model model,@PathVariable("id")Long id){
        model.addAttribute("taskStudent",studentService.getById(id));
        List<Status> statusList = statusService.list();
        model.addAttribute("statusList", statusList);
        return prefix+"edit";
    }

    @PostMapping("edit")
    @ResponseBody
    public AjaxResult edit(TaskStudent taskStudent){
        return toAjax(studentService.updateById(taskStudent));
    }
    /**
    * 删除
    * @param id
    * @return
    */
    @GetMapping("delete/{id}")
    @ResponseBody
    public AjaxResult delete(@PathVariable("id") Long id){
        return toAjax(studentService.removeById(id));
    }
    /**
    * 批量删除
    * @param ids
    * @return
    */
    @PostMapping("deleteAll")
    @ResponseBody
    public AjaxResult deleteAll(@RequestBody List<Long> ids){
        return toAjax(studentService.removeByIds(ids));
    }

}

